<?php

namespace JsonLowCode\Traits\Props;

use Closure;
use JsonLowCode\Support\JsParse;
use JsonLowCode\Support\VueDirective;

trait HasModelValue
{

    /**
     * 双向绑定
     * @param string $modelName
     * @param string $bindName
     * @param mixed|null $defaultValue
     * @return $this
     */
    public function bindModel(string $modelName = "", string $bindName = "", mixed $defaultValue = null):static
    {
        if ($defaultValue!==null){
            $this->setState(str_replace(["this.","state."],"", $bindName), $defaultValue);
        }
        VueDirective::vModel($this, $bindName, $modelName);
        return $this;
    }

    /**
     * @param string $bindName
     * @param mixed|null $defaultValue
     * @return $this
     */
    public function modelValue(string $bindName, mixed $defaultValue = null):static
    {
        return $this->bindModel("modelValue",$bindName, $defaultValue);
    }
}
